package com.shoppingmall.mainfragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewpager.widget.ViewPager;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.androidkun.xtablayout.XTabLayout;
import com.example.zyh.toolslib.base.BaseFragment;
import com.ms.banner.Banner;
import com.ms.banner.listener.OnBannerClickListener;
import com.shoppingmall.R;
import com.shoppingmall.activity.home.ClassficationActivity;
import com.shoppingmall.activity.home.GoodListActivity;
import com.shoppingmall.activity.home.SearchActivty;
import com.shoppingmall.activity.home.SearchResultActivity;
import com.shoppingmall.activity.home.ShopListactivity;
import com.shoppingmall.adapter.CommonAdapter;
import com.shoppingmall.adapter.HomeGoodAdepter;
import com.shoppingmall.adapter.TabAdepter;
import com.shoppingmall.bean.HomeBannerbean;
import com.shoppingmall.bean.HomeClassBean;
import com.shoppingmall.bean.LoginBean;
import com.shoppingmall.bean.TabBean;
import com.shoppingmall.data.DataApi;
import com.shoppingmall.fragment.HomeGoodFragment;
import com.shoppingmall.login.LoginActivity;
import com.shoppingmall.widget.CustomViewHolder;
import com.shoppingmall.widget.RecycleGridDivider;
import com.shoppingmall.widget.ViewPagerForScrollView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * liangwanbo
 */
public class HomeFragment extends BaseFragment {


    @BindView(R.id.image_banner)
    Banner imageBanner;
    @BindView(R.id.lin_csgood)
    LinearLayout linCsgood;
    @BindView(R.id.lin_fxgood)
    LinearLayout linFxgood;
    @BindView(R.id.lin_moreclass)
    LinearLayout linMoreclass;
    @BindView(R.id.lin_business_list)
    LinearLayout linBusinessList;
    @BindView(R.id.lin_cuncuntong)
    LinearLayout linCuncuntong;
    @BindView(R.id.lin_leibie)
    LinearLayout linLeibie;
    @BindView(R.id.re_commend)
    RelativeLayout reCommend;
    @BindView(R.id.luck_draw)
    ImageView luckDraw;
    @BindView(R.id.image_recharge)
    ImageView imageRecharge;
    @BindView(R.id.luck)
    ImageView luck;
    @BindView(R.id.lin_chouse)
    LinearLayout linChouse;
    @BindView(R.id.xTablayout)
    XTabLayout xTablayout;
    @BindView(R.id.view_pager)
    ViewPagerForScrollView viewPager;
    @BindView(R.id.image_xiaoxi)
    ImageView imageXiaoxi;
    @BindView(R.id.re_xiaoxi)
    RelativeLayout reXiaoxi;
    @BindView(R.id.image_search)
    ImageView imageSearch;
    @BindView(R.id.re_search)
    RelativeLayout reSearch;
    private String[] title;
    private List<HomeClassBean.DataBean> classlist=new ArrayList<>();
    private List<BaseFragment> fragmentList;
    private int selectPos = 0;
    private List<String> listbanner=new ArrayList<>();
    @Override
    protected int initLayout() {
        return R.layout.fragment_home;
    }


    @Override
    protected void initView(View rootView) {

    }


    @Override
    protected void initData() {
        fragmentList=new ArrayList<>();
        getclassdata();
        getBannerdata("mobile");


    }


    @OnClick({R.id.re_xiaoxi, R.id.re_search, R.id.lin_csgood, R.id.lin_fxgood, R.id.lin_moreclass, R.id.lin_business_list, R.id.lin_cuncuntong, R.id.luck_draw, R.id.image_recharge, R.id.luck})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.re_xiaoxi:
                break;
            case R.id.re_search:
                Intent intent = new Intent(getActivity(), SearchActivty.class);
                startActivity(intent);
                break;
            case R.id.lin_csgood:
                Intent intentgood = new Intent(getActivity(), GoodListActivity.class);
                startActivity(intentgood);
                break;
            case R.id.lin_fxgood:
                Intent intentgoodfy = new Intent(getActivity(), GoodListActivity.class);
                startActivity(intentgoodfy);
                break;
            case R.id.lin_moreclass:
                Intent intentclass = new Intent(getActivity(), ClassficationActivity.class);
                startActivity(intentclass);
                break;
            case R.id.lin_business_list:
                Intent intentshop = new Intent(getActivity(), ShopListactivity.class);
                startActivity(intentshop);
                break;
            case R.id.lin_cuncuntong:
                break;
            case R.id.luck_draw:
                Intent intentLogin = new Intent(getActivity(), LoginActivity.class);
                startActivity(intentLogin);
                break;
            case R.id.image_recharge:
                break;
            case R.id.luck:
                Intent intentluck = new Intent(getActivity(), GoodListActivity.class);
                startActivity(intentluck);
                break;

        }
    }




    private class ViewPagerAdapter extends FragmentPagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }
    }


    public interface OnModifyListener {
        void modify(String type);
    }
    /**
     * 广告
     */

    public void getBannerdata(String mobile){
        RxHttpUtils.createApi(DataApi.class)
                .HomeBanner(mobile)
                .compose(Transformer.<HomeBannerbean>switchSchedulers())
                .subscribe(new CommonObserver<HomeBannerbean>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(HomeBannerbean homeBannerbean) {
                        Log.e("homeBannerbean",homeBannerbean.toString()+"");
                        for(int i=0;i<homeBannerbean.getData().size();i++){
                            listbanner.add(homeBannerbean.getData().get(i).getPic_url());
                        }
                        imageBanner.setAutoPlay(true)
                                .setPages(listbanner, new CustomViewHolder())
                                .setDelayTime(3000)
                                .setOnBannerClickListener(new OnBannerClickListener() {
                                    @Override
                                    public void onBannerClick(List datas, int position) {
                                        Toast.makeText(getActivity(), "你点击了：" + position, Toast.LENGTH_SHORT).show();
                                    }
                                })
                                .start();

                    }
                });

    }

    /**
     * 分类标题
     */

    public void getclassdata(){
        RxHttpUtils.createApi(DataApi.class)
                .secondLevel()
                .compose(Transformer.<HomeClassBean>switchSchedulers())
                .subscribe(new CommonObserver<HomeClassBean>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(HomeClassBean body) {
                        Log.e("body",body.toString()+"");
                        title=new String[body.getData().size()];
                        for(int i=0;i<body.getData().size();i++){
                            title[i]=body.getData().get(i).getName();
                            fragmentList.add(new HomeGoodFragment(body.getData().get(i).getCategory_id()+""));
                        }


                        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
                        viewPager.setOffscreenPageLimit(title.length);
                        viewPager.setAdapter(viewPagerAdapter);
                        xTablayout.setupWithViewPager(viewPager);
                        xTablayout.getTabAt(selectPos).select();
                    }
                });

    }
}
